//
//  CZEmoticonCollectionViewCell.swift
//  weiBo
//
//  Created by apple on 16/5/22.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit
let DidSeletedEmoticonNotification = "SeletedEmoticon"

class CZEmoticonCollectionViewCell: UICollectionViewCell {
    //按钮数组
    private lazy var emoticonButtons: [CZEmoticonButton] = [CZEmoticonButton]()

    //懒加载删除按钮
    private lazy var deleteEmoticonButton: UIButton = {
        let button = UIButton()
        button.addTarget(self, action: "deleteEmoticonButtonClick", forControlEvents: .TouchUpInside)
        button.setImage(UIImage(named: "compose_emotion_delete"), forState: .Normal)
        button.setImage(UIImage(named: "compose_emotion_delete_highlighted"), forState: .Highlighted)
        
        return button
    }()
    //定义表情数组
    var emotions: [CZEmoticon]? {
        didSet{
            guard let ets = emotions else {
                return
            }
            
            for (i, value) in ets.enumerate() {
                //取到对应的btn
                let btn = emoticonButtons[i]
                btn.emotion = value
           
            }
        
        }
    
    }
    
    var indexPath: NSIndexPath? {
        didSet {
            guard let index = indexPath else {
                return
            }
            
            messageLabel.text = "当前显示的是第\(index.section)组第\(index.item)行"
        }
    
    }
    
    private lazy var messageLabel: UILabel = {
        let label = UILabel(textColor: UIColor.whiteColor(), fontSize: 30)
        return label
    }()

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setUpUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setUpUI() {
    
        contentView.addSubview(messageLabel)
        contentView.addSubview(deleteEmoticonButton)
        
        messageLabel.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(contentView)
        }
        
        //添加表情btn
        addChildrenBtn()
    }
    
    private func addChildrenBtn() {
        for _ in 0 ..< 20 {
            let btn = CZEmoticonButton()
            btn.addTarget(self, action: "emoticonClick:", forControlEvents: .TouchUpInside)
            btn.titleLabel?.font = UIFont.systemFontOfSize(34)
            //  添加到contentview里面
            contentView.addSubview(btn)
            emoticonButtons.append(btn)

            
            
        }
    }
    override func layoutSubviews() {
        super.layoutSubviews()
        //  设置表情按钮的frame
        //  表情按钮的宽度
        let itemWidth = width / 7
        //  表情按钮的高度
        let itemHeight = height / 3
        //  遍历数组 i 代表数组的下标
        for (i, value) in emoticonButtons.enumerate() {
            //  设置按钮的大小
            value.size = CGSize(width: itemWidth, height: itemHeight)
            //  计算col索引
            let colIndex = i % 7
            //  计算row索引
            let rowIndex = i / 7
            //  设置x坐标
            value.x = CGFloat(colIndex) * itemWidth
            //  设置y坐标
            value.y = CGFloat(rowIndex) * itemHeight
     
        }
        
        deleteEmoticonButton.size = CGSizeMake(itemWidth, itemHeight)
        deleteEmoticonButton.x = width - itemWidth
        deleteEmoticonButton.y = height - itemHeight
        
    }
    
    @objc func emoticonClick(btn: CZEmoticonButton) {
    
        NSNotificationCenter.defaultCenter().postNotificationName(DidSeletedEmoticonNotification, object: btn.emotion)
    
    
    }
    
    @objc func deleteEmoticonButtonClick() {
        NSNotificationCenter.defaultCenter().postNotificationName(DidSelectedDeleteEmoticonButtonNotification, object: nil)
    
    }
    
}
